home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12113 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  131 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: How to get at base class fields from member functions?
  5. Message-ID: <marnoldDoGEGI.G09@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com> <internews46B0766104@argonet.co.uk>
  8. Date: Mon, 18 Mar 1996 08:05:06 GMT
  9. Sender: marnold@netcom4.netcom.com
  10.  
  11. Dave Mullard <dmullard@argonet.co.uk> writes:
  12.  
  13. >Chris Cobb <ccobb@erols.com> wrote:
  14. >> I <dmullard@argonet.co.uk> wrote:
  15. >> >Given three classes A, B and C I want to have multiple As for each B
  16. >> and
  17. >> >multiple Bs for each C. To do this I could create the following.
  18. >> >
  19. >> >class b1 : public B
  20. >> >{
  21. >> >A a1;
  22. >> >A a2;
  23. >> >A a3;
  24. >> >};
  25.  
  26. >> >class b2 : public B
  27. >> >{
  28. >> >A a1;
  29. >> >A a2;
  30. >> >A a3;
  31. >> >A a4;
  32. >> >A a5;
  33. >> >};
  34.  
  35. >> >class c1 : public C
  36. >> >{
  37. >> >b1 b1;
  38. >> >b2 b2;
  39. >> >};
  40.  
  41. >> >The question is, how within functions for class B do I access fields
  42. >> >within class C, and similarly, how within the class A functions do I
  43. >> >access fields within class B?
  44.  
  45.  
  46. >> You have encountered the wall of encapsulation.  It is considered poor 
  47. >> form to violate this wall without good reason.  The need to violate this
  48. >> wall may indicate poor design.  However, C++ is not so rigid as to 
  49. >> disallow this.  The way this is done is to have the class whose members 
  50. >> need to be accessed declare the class who wants to access them a friend:
  51. >> class C
  52. >> {
  53. >>    friend class B;
  54. >>    // ...
  55. >> };
  56. >> class B
  57. >> {
  58. >>    friend class A;
  59. >>    // ...
  60. >> };
  61. >I obviously have not explained myself very well.
  62.  
  63. >Maybe instead of *access* I should have said identify.
  64.  
  65. >If, for example, B is
  66.  
  67. >class B
  68. >{
  69. >friend class A; 
  70. >int fred;
  71. >}
  72.  
  73. >I cannot just
  74.  
  75. >A::A() {fred=21;};
  76.  
  77. >or even 
  78.  
  79. >A::A() {B::fred=21;};
  80.  
  81. >The As are only members of a class that has B as a base.
  82.  
  83. >I suppose at its simplest level the problem is this
  84.  
  85. >class B
  86. >{
  87. >int fred;
  88. >   class A
  89. >   {
  90. >      A() { };
  91. >   };
  92. >A a1;
  93. >A a2;
  94. >};
  95.  
  96. >How within A::A do I refer to fred. 
  97.  
  98. You don't.  
  99.  
  100. Realize that, even though A is nested class of B, A and B are distinct 
  101. types and not "linked" in the way you seen to think they are.  In your 
  102. sample code above, A has no more connection to or knowledge of B than 
  103. some other class C in a totally different part of your program.
  104.  
  105. If you need this kind of link, you must explicitly code it yourself.  
  106. For example, you could arrange to pass, to an instance of A, a 
  107. reference or pointer to an instance B, and access B::fred in *that* 
  108. instance.
  109.  
  110. A could have the following member...
  111.  
  112. void A::access_fred(B* b)
  113.    { 
  114.    b.fred = 42;
  115.    }
  116.  
  117.  
  118.  
  119. Regards,
  120. -------------------------------------------------------------------------
  121. Matt Arnold                       |        | ||| | |||| |  | | || ||
  122. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  123. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  124. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  125. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  126. -------------------------------------------------------------------------
  127.